package com.WISSC.controller;


import com.WISSC.bean.Configure;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.service.ConfigureService;
import com.WISSC.util.Respons.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Map;

/**
 * <p>
 * 配置值
 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-06-06
 */
@Controller
@RequestMapping("/configure")
public class ConfigureController {


    @RequestMapping(value = "configure.html",method = RequestMethod.GET)
    public String configure(){
        return "/system/configure/configure";
    }

    @RequestMapping(value = "configure_edit.html",method = RequestMethod.GET)
    public String configure_edit(){
        return "/system/configure/configure_edit";
    }

    @RequestMapping(value = "configure_file.html",method = RequestMethod.GET)
    public String configure_file(Model model){
        model.addAttribute("type",configureService.getById(1));
        return "/system/configure/configure_file";
    }


    @Autowired
    ConfigureService configureService;

    /**
     * @param page
     * @param limit
     * @return 查询
     */
    @RequestMapping(value = "select",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Configure> select(int page,int limit){
        return configureService.selectConfigure(page,limit);
    }


    /**
     * @param configure
     * @return 修改
     */
    @RequestMapping(value = "updateByIdConfigure",method = RequestMethod.POST)
    @ResponseBody
    public Map updateByIdConfigure(@RequestBody Configure configure){
            if(configureService.updateById(configure)){
                return ResponseUtil.getSuccessMap();
            }
            return ResponseUtil.getExceptionMap();
    }

    //获取图片
    @RequestMapping("/getPhoto")
    public void getPhoto(HttpServletResponse response, @RequestParam("path") String path) throws Exception {
        System.out.println("path= " + path);
        File file = new File(path);
        FileInputStream fis;
        fis = new FileInputStream(file);
        long size = file.length();
        byte[] temp = new byte[(int) size];
        fis.read(temp, 0, (int) size);
        fis.close();
        byte[] data = temp;
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        out.write(data);
        out.flush();
        out.close();
    }

}
